package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.common.service.CrudService;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.base.web.FSysController;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.RentproductProcedureDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TDemandsDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.ProSenddemandsms;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;

/**
 * 用户需求Service
 */
@Service
@Transactional(readOnly = true)
public class FDemandsService extends CrudService<TDemandsDao, TDemands> {
	
	@Autowired
	private RentproductProcedureDao rentproductProcedureDao;
	
	public TDemands get(String demandid) {
		return super.get(demandid);
	}
	
	public List<TDemands> findList(TDemands tDemands) {
		return super.findList(tDemands);
	}
	
	public Page<TDemands> findPage(Page<TDemands> page, TDemands tDemands) {
		return super.findPage(page, tDemands);
	}
	
	@Transactional(readOnly = false)
	public void save(TDemands tDemands) {
		if(tDemands.getDemandid() == null) {
			dao.insert(tDemands);
			ProSenddemandsms proSenddemandsms = new ProSenddemandsms();
			proSenddemandsms.setDemandid(tDemands.getDemandid());
			Map<String, Object> map = proSenddemandsms.toMap().getParam();
			rentproductProcedureDao.proSenddemandsms(map);//{demandid=148}
		} else {
			dao.update(tDemands);
		}
	}
	
	@Transactional(readOnly = false)
	public void cancel(TDemands tDemands) {
		tDemands= dao.get(""+tDemands.getDemandid());
		tDemands.setReactstatus(TDemands.REACTSTATUS_CANCEL);
		DemandsReact dr = new DemandsReact();
		dr.setDemandid(tDemands.getDemandid());
		dao.cancelReact(dr);
		dao.update(tDemands);
	}
	
	@Transactional(readOnly = false)
	public void delete(TDemands tDemands) {
		super.delete(tDemands);
	}
	
	public List<DemandsReact> findDemandsReactList(TDemands tDemands){
		return dao.findDemandsReactList(tDemands);
	}
	
	public DemandsReact getDemandsReact(Integer reactid){
		return dao.getDemandsReact(reactid);
	}

	public void updateState(String demandsid) {
		dao.updateState(demandsid);
	}

	public String getSendAddress(String demandid) {
		return dao.getSendAddress(demandid);
	}

	public void deleteDemand(Integer demandid) {
		dao.deleteDemand(demandid);
	}

	public TDemands getDemandById(Integer demandid) {
		return dao.getDemandById(demandid);
	}

	public void updateOrderId(String orderid, Integer demandid) {
		dao.updateOrderId(orderid,demandid);
	}

}